package com.chagee.store.operation.server.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.chagee.store.operation.common.exception.BizErrorCode;
import com.chagee.store.operation.common.exception.BizException;

import java.text.*;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @Author : hk
 * @Date : 2024/10/29
 */
public class DateUtil {

	/**
	 * 日期时间格式
	 */
	public final static String DEFAULT_DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

	/**
	 * 日期格式
	 */
	public final static String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";

	/**
	 * 年月日期格式
	 */
	public final static String YEARS_DATE_PATTERN = "yyyy-MM";

	public final static String DATE_PATTERN = "yyyyMMddHHmmss";

	public final static String EMAIL_DATE_PATTERN = "yyyy年MM月dd日";

	/**
	 * 获取指定日期-月初
	 */
	public static String getStartOfMonth(String date, String pattern) throws Exception {
		Date startDate = parse(date, pattern);
		Date startOfMonth = getStartOfMonth(startDate);
		SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_TIME_PATTERN);
		String format = dateFormat.format(startOfMonth);
		return format;
	}

	/**
	 * 获取指定日期-月末
	 */
	public static String getEndOfMonth(String date, String pattern) throws Exception {
		Date EndDate = parse(date, pattern);
		Date endOfMonth = getEndOfMonth(EndDate);
		SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_TIME_PATTERN);
		String format = dateFormat.format(endOfMonth);
		return format;
	}

	public static Date getStartOfMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	public static Date getEndOfMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		return calendar.getTime();
	}
	public static Boolean getDistanceDate(String startDateTime, String endDateTime) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_TIME_PATTERN);
		try {
			Date startDate = dateFormat.parse(startDateTime);
			Date endDate = dateFormat.parse(endDateTime);
			LocalDateTime startTime = LocalDateTimeUtil.of(startDate);
			LocalDateTime endTime = LocalDateTimeUtil.of(endDate);

			long seconds = Duration.between(startTime, endTime).getSeconds();
			long time = 31 * 24 * 60 * 60;
			if (seconds > time) {
				return false;
			} else {
				return true;
			}
		} catch (Exception e) {
			throw new BizException(BizErrorCode.DATE_CONVERT_FAIL);
		}
	}

	public static int getMonthNum(String dateStr) {
		Integer year = Integer.valueOf(dateStr.substring(0, 4));
		Integer month = Integer.valueOf(dateStr.substring(5, 7));
		LocalDate date = LocalDate.of(year, month, 1);
		return date.lengthOfMonth();
	}


	public static Boolean getOrderExpiredDate(Date date, Long hours) {
		if (Objects.isNull(date)) {
			return false;
		}
		LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();

		LocalDateTime dateTime = LocalDateTime.now();
		//指定小时前时间
		LocalDateTime dateTime1 = dateTime.minusHours(hours);
		return dateTime1.isBefore(localDateTime);
	}

	//获取昨天开始时间
	public static String getYearsDate() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY,0);
		calendar.set(Calendar.MINUTE,0);
		calendar.set(Calendar.SECOND,0);
		// 将日期向前调整一天（即昨天）
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		// 格式化日期输出
		String yesterday = new SimpleDateFormat(DEFAULT_DATE_TIME_PATTERN).format(calendar.getTime());
		return yesterday;
	}

	/***
	 *　获取指定日期前一日
	 * @return
	 */
	public static String getYearsDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// 将日期向前调整一天（即昨天）
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		// 格式化日期输出
		String yesterday = new SimpleDateFormat(DEFAULT_DATE_PATTERN).format(calendar.getTime());

		return yesterday;
	}


	public static String format(Date date, String pattern) {
		if (date == null) {
			return null;
		}
		SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
		String d = dateFormat.format(date);
		return d;
	}

	public static String getTimestamp() {
		DateTime timestamp = DateTime.now();
		String formattedTimestamp = timestamp.toString(DATE_PATTERN);
		return formattedTimestamp;
	}

	public static String format(Date date) {
		return format(date, DEFAULT_DATE_TIME_PATTERN);
	}

	public static Date parse(String date, String pattern) throws ParseException {
		if (date == null || "".equals(date)) {
			return null;
		}
		SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
		Date d = dateFormat.parse(date);
		return d;
	}

	public static Date parse(String date) throws ParseException {
		return parse(date, DEFAULT_DATE_TIME_PATTERN);
	}

	//获取昨天开始时间
	public static String yesterDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY,0);
		calendar.set(Calendar.MINUTE,0);
		calendar.set(Calendar.SECOND,0);
		// 将日期向前调整一天（即昨天）
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		// 格式化日期输出
		String yesterday = new SimpleDateFormat(DEFAULT_DATE_TIME_PATTERN).format(calendar.getTime());
		return yesterday;
	}

	public static String yesterdayStr(String pattern) {
		LocalDate yesterday = yesterday();
		return yesterday.format(DateTimeFormatter.ofPattern(pattern));
	}

	public static LocalDate yesterday() {
		LocalDate now = LocalDate.now();
		return now.plusDays(-1L);
	}

	//获取指定日期前 一个月
	public static Date getFrontMonth(Date date) {
		SimpleDateFormat sd = new SimpleDateFormat(DateUtil.DEFAULT_DATE_TIME_PATTERN);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, -1);
		String format = sd.format(calendar.getTime());
		Date lastMonth = null;
		try {
			lastMonth = sd.parse(format);
		} catch (ParseException e) {
			throw new BizException(BizErrorCode.DATE_CONVERT_FAIL);
		}
		return lastMonth;
	}

	public static Boolean dateCompareTo(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date startDate = null;
		try {
			startDate = sdf.parse("2025-04-01 00:00:00");
		} catch (ParseException e) {
			throw new BizException(BizErrorCode.DATE_CONVERT_FAIL);
		}
		if (date.compareTo(startDate) > 0) {
			return true;
		} else {
			return false;
		}
	}
}
